<?php

namespace app\controller;

use app\BaseController;
use app\model\Site;
use think\facade\Db;
use think\facade\Session;
use think\facade\Request;
use app\utils\Utils;
use mailer\facade\Mailer;
use app\model\Log;
use think\facade\Cookie;

class Api extends BaseController
{
    static public function get_banner() // 获取首页轮播数据
    {
        header('Content-Type: application/json');

        $result = Db::table('site_banner')->select()->toArray(); // 查询数据
        return Utils::api_response(200, "获取成功", $result);
    }

    static public function logout() // 退出登录
    {
        $email = Session::get('email', '');
        $pw = Session::get('password', '');
        $my_user_info = Utils::user($email, $pw);
        if ($my_user_info['id'] == '') {
            //未登录
            Session::clear();
            return json(array("code" => 403, 'msg' => '未登录'));
        }

        Db::table('site_token')->where('user_id', $my_user_info['id'])->delete();

        //过滤token的非法字符，只允许字母和数字
        $keyID = preg_replace('/[^a-zA-Z0-9]/', '', Cookie::get("PHPSESSID"));
        unlink("../runtime/session/sess_" . $keyID);
        Session::clear();

        return Utils::api_response(200, "退出成功");
    }

    static public function get_new_things() // 新产品系统
    {
        header('Content-Type: application/json');

        $result = Db::table('site_new_things')->select()->toArray(); // 查询数据
        return Utils::api_response(200, "获取成功", $result);
    }

    static public function get_activities() // 活动系统
    {
        header('Content-Type: application/json');

        $result = Db::table('site_activity')->select()->toArray(); // 查询数据
        return Utils::api_response(200, "获取成功", $result);
    }
}
